package com.uyun.rbac.permission.service.impl;


import com.uyun.rbac.permission.dao.PermissionDao;
import com.uyun.rbac.permission.entity.MenuEntity;
import com.uyun.rbac.permission.entity.PermissionEntity;
import com.uyun.rbac.permission.entity.RoleEntity;
import com.uyun.rbac.permission.entity.UserEntity;
import com.uyun.rbac.permission.service.PermissionService;
import com.uyun.rbac.shiro.IAuthService;
import com.uyun.rbac.user.service.UserService;
import com.uyun.util.AppUtils;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.DefaultFilterChainManager;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service
public class PermissionServiceImpl implements PermissionService {
	
	@Autowired
	private PermissionDao permissionDao;
    @Autowired
    private UserService userService;
    @Autowired
    private IAuthService authService;
    @Autowired
    private ShiroFilterFactoryBean shiroFilterFactoryBean;


    /** for permission page **/

    @Override
    public List<PermissionEntity> getPermissionListForPage(Map<String, String> paraMap){

        return permissionDao.getPermissionListForPage(paraMap);

    }

    @Override
    public boolean addPermissionEntity(PermissionEntity entity){
        return permissionDao.addPermissionEntity(entity);
    }


    /** for role page **/
    @Override
    public List<RoleEntity> getRoleListForPage(Map<String, String> paraMap){

        return permissionDao.getRoleListForPage(paraMap);

    }


    @Override
    public RoleEntity getRoleEntityById(int id){

        return permissionDao.getRoleEntityById(id);

    }

    @Override
    public boolean editRoleHasMenu(String roleId, String[] menuIds){

        try{
            permissionDao.clearRoleHasMenu(Integer.parseInt(roleId));
        }catch (Exception e){
            return false;
        }

        for(String menuId:menuIds){

            try{
                permissionDao.addRoleHasMenu(Integer.parseInt(roleId),Integer.parseInt(menuId));
            }catch (Exception ignored){}

        }

        authService.reCreateFilterChains(shiroFilterFactoryBean);

        return true;

    }

    @Override
    public boolean editRoleHasPerms(String roleId, String[] permIds) {

        try{
            permissionDao.clearRoleHasPerm(Integer.parseInt(roleId));
        }catch (Exception e){
            return false;
        }

        for(String permId:permIds){

            try{
                permissionDao.addRoleHasPerm(Integer.parseInt(roleId),Integer.parseInt(permId));
            }catch (Exception ignored){}

        }
        authService.reCreateFilterChains(shiroFilterFactoryBean);

        return true;

    }


    @Override
    public List<RoleEntity> getAllRoleEntity(){

        List<RoleEntity> list = new ArrayList<>();

        List<Integer> roleIds = permissionDao.getAllRoleIds();


        roleIds.stream().forEach(roleId ->list.add(permissionDao.getRoleEntityById(roleId)));

        return list;

    }

    @Override
    public List<PermissionEntity> getAllPermissionEntity(){

        List<Integer> permIds = permissionDao.getAllPermissionIds();
        List<PermissionEntity> list = new ArrayList<>();
        permIds.stream().forEach(permId->list.add(permissionDao.getPermissionEntityById(permId)));

        return list;

    }



    /**
     * 以下部分为菜单的获取
     * @param userName
     * @return
     */
    @Override
    public List<MenuEntity> getMenuList(String userName){


        UserEntity userEntity = userService.getUserEntityByUserName(userName);

        List<MenuEntity> menuList = new ArrayList<>();

        Set<Integer> menuIds = new HashSet<>();

        if(userEntity!=null&&userEntity.getGroupEntity()!=null&&
                userEntity.getGroupEntity().getRoleEntityList()!=null&&!userEntity.getGroupEntity().getRoleEntityList().isEmpty()){

            List<RoleEntity> roleEntityList = userEntity.getGroupEntity().getRoleEntityList();

            for(RoleEntity roleEntity:roleEntityList){

                List<MenuEntity> menuEntityList = roleEntity.getMenuEntityList();


                if(menuEntityList!=null&&!menuEntityList.isEmpty()){


                    for(MenuEntity menuEntity:menuEntityList){

                        menuIds.add(menuEntity.getId());

                        if(menuEntity.getChildren()!=null&&!menuEntity.getChildren().isEmpty()){

                            menuIds.addAll(menuEntity.getChildren().stream().map(MenuEntity::getId).collect(Collectors.toList()));

                        }// end if  menuEntity.getChildren()!=null

                    }// end foreach MenuEntity


                }// end menuEntityList!=null


            }// end foreach RoleEntity


        }// end userEntity whole is not null

        if(!menuIds.isEmpty()){
            menuList.addAll(menuIds.stream().map(menuId -> permissionDao.getMenuEntityById(menuId)).collect(Collectors.toList()));
        }

        for(MenuEntity entity:menuList){
            AppUtils.buildTree(menuList,entity);
        }

        return menuList.stream().filter(entity -> entity.getPid() == null||entity.getPid()==0).collect(Collectors.toList());
    }

    @Override
    public List<MenuEntity> getAllMenu(){


        List<MenuEntity> menuList = permissionDao.getAllMenuList();

        for(MenuEntity entity:menuList){
            AppUtils.buildTree(menuList,entity);
        }

        return menuList.stream().filter(entity -> entity.getPid() == null||entity.getPid()==0).collect(Collectors.toList());

    }



}
